package com.cincc.cincc_pbx.common;

import android.content.Context;
import android.content.SharedPreferences;

import com.cincc.cincc_pbx.GlobalApp;
import com.cincc.cincc_pbx.constant.AttrsConstant;

public class SharePreferenceUtil {

    private SharedPreferences preferences;
    private SharedPreferences.Editor editor;
    public static String SP_NAME = "cincc";
    private static SharePreferenceUtil instance;

    private SharePreferenceUtil() {
        preferences = GlobalApp.context.getSharedPreferences(SP_NAME, Context.MODE_PRIVATE);
        editor = preferences.edit();
    }

    /**
     * 单例模式 双重检查锁定
     *
     * @return
     */
    public static SharePreferenceUtil getInstance() {
        if (instance == null) {
            synchronized (SharePreferenceUtil.class) {
                if (instance == null) {
                    return new SharePreferenceUtil();
                }
            }
        }

        return instance;
    }


    /**
     * 保存数据的方法，我们需要拿到保存数据的具体类型，然后根据类型调用不同的保存方法
     *
     * @param key
     * @param value
     */
    public void setValue(String key, Object value) {

        String type = value.getClass().getSimpleName();

        if (AttrsConstant.STRING.equals(type)) {
            editor.putString(key, (String) value);
        } else if (AttrsConstant.INTEGER.equals(type)) {
            editor.putInt(key, (Integer) value);
        } else if (AttrsConstant.BOOLEAN.equals(type)) {
            editor.putBoolean(key, (Boolean) value);
        } else if (AttrsConstant.FLOAT.equals(type)) {
            editor.putFloat(key, (Float) value);
        } else if (AttrsConstant.LONG.equals(type)) {
            editor.putLong(key, (Long) value);
        }

        editor.commit();
    }

    /**
     * 得到保存数据的方法，我们根据默认值得到保存的数据的具体类型，然后调用相对于的方法获取值
     *
     * @param key
     * @return
     */
    public Object getValue(String key, Object defaultValue) {

        String type = defaultValue.getClass().getSimpleName();

        if (AttrsConstant.STRING.equals(type)) {
            return preferences.getString(key, (String) defaultValue);
        } else if (AttrsConstant.INTEGER.equals(type)) {
            return preferences.getInt(key, (Integer) defaultValue);
        } else if (AttrsConstant.BOOLEAN.equals(type)) {
            return preferences.getBoolean(key, (Boolean) defaultValue);
        } else if (AttrsConstant.FLOAT.equals(type)) {
            return preferences.getFloat(key, (Float) defaultValue);
        } else if (AttrsConstant.LONG.equals(type)) {
            return preferences.getLong(key, (Long) defaultValue);
        }

        return null;
    }

    /**
     * 清空SP里所有数据 谨慎调用
     */
    public void clear() {
        editor.clear();//清空
        editor.commit();//提交
    }

    /**
     * 删除SP里指定key对应的数据项
     *
     * @param key
     */
    public void remove(String key) {
        //删除掉指定的值
        editor.remove(key);
        //提交
        editor.commit();
    }

    /**
     * 查看sp文件里面是否存在此 key
     *
     * @param key
     * @return
     */
    public boolean contains(String key) {
        return preferences.contains(key);
    }

}
